<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class health_info_forge
{
	var $CI;

	public function __construct()
	{
		$this->CI = &get_instance();
	}

	public function remove_user_health_info($field)
	{
		$this->CI->dbforge->drop_column("userhealthinfo", $field);
	}

	public function remove_health_field_meta($field)
	{
		$this->CI->db->where("columnName", $field);
		$this->CI->db->delete("healthfieldmeta");
	}
	
	public function remove_user_share($field)
	{
		$this->CI->dbforge->drop_column("usershare", $field);
	}

	public function add_user_health_info($field, $type_code)
	{
		$default = null;
		$type = get_health_info_type($type_code);
		
		$fieldInfo = null;
		switch($type)
		{
			case "BOOL":
				$default = 0;
				break;
				
			case "INT":
				$default = 0;
				break;
				
			case "TEXT":
				$default = "";
				break;
				
			case "FLOAT":
				$default = 0.0;
				break;
				
			default:
				break;
		}
		
/*  		if($type == "VARCHAR")
		{
			$fieldInfo = array(
					$field=>array(
							"type"=>$type,
							"constraint"=>"256",
							"default"=>""
					)
			);
			$this->CI->dbforge->add_column("UserHealthInfo", $fieldInfo);
			
			$this->CI->db->query("alter table userhealthinfo modify $field varchar(256) character set utf8");
			
			return;
		} */ 
		
		$fieldInfo = array(
				$field=>array(
						"type"=>$type,
						"default"=>$default,
				)
		);
		
		$this->CI->dbforge->add_column("userhealthinfo", $fieldInfo);
	}

	public function add_health_field_meta($field, $type, $desc)
	{
		$record = array(
				"columnName"=>$field,
				"description"=>$desc,
				"columnType"=>$type,
		);

		$this->CI->db->insert("healthfieldmeta", $record);
	}
	
	public function add_user_share($field)
	{
		$fieldInfo = array(
				$field=>array(
						"type"=>"BOOL",
						"default"=>0,
				)
		);
		$this->CI->dbforge->add_column("usershare", $fieldInfo);
	}
}

class user_forge
{
	var $CI;

	public function __construct()
	{
		$this->CI = &get_instance();
	}

	public function remove_friend_request()
	{
		$this->CI->db->query("truncate userfriendrequest;");
	}
	
	public function remove_share_info($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("usershare");
	}

	public function remove_health_info($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("userhealthinfo");
	}

	public function remove_base_info($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("userbaseinfo");
	}

	public function remove_friend($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("userfriends");

		$this->CI->db->where("friendUid");
		$this->CI->db->delete("userfriends");
	}

	public function remove_health_info_push($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("userhealthinfopushnotice");
	}

	public function remove_symptom($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("usersymptom");
	}

	public function remove_symptom_push($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("usersymptompushnotice");
	}

	public function remove_login($uid)
	{
		$this->CI->db->where("uid", $uid);
		$this->CI->db->delete("userlogin");
	}
}

class test_data_forge
{
	var $CI;

	public function __construct()
	{
		$this->CI= &get_instance();
	}

	private function gen_user_login($prefix)
	{
		static $id = 0;
		$user_name = $prefix.($id++);

		$record = array(
				"nickname"=>$user_name,
				"password"=>md5("123456"),
				"lastloginat"=>mysql_datetime()
		);
		$this->CI->db->insert("userlogin", $record);
		$uid = $this->CI->db->insert_id();

		return array($user_name, $uid);
	}

	private function gen_user_base_info($uid)
	{
		$birthday = mysql_datetime();
		$sex = rand(0, 1);
		$city = rand(0, 1);
		$firstName =  $uid;
		$lastName = "测试用户";
		$regDate = mysql_datetime();

		$record = array(
				"uid"=>$uid,
				"birthday"=>$birthday,
				"sex"=>$sex,
				"city"=>$city,
				"firstName"=>$firstName,
				"lastName"=>$lastName,
				"regDate"=>$regDate
		);

		$this->CI->db->insert("userbaseinfo", $record);
	}


	public function do_data_generation($num, $prefix)
	{
		$num = (int)$num;
		for($i = 0; $i < $num; $i++)
		{
			list($user_name, $uid) = $this->gen_user_login($prefix);
			$this->gen_user_base_info($uid);
			$this->gen_user_health_info($uid);
		}
	}

	private function gen_user_health_info($uid)
	{
		for($i = 20; $i > 0; $i--){
			$height = rand(150, 180);
			$weight = rand(50, 100);
			$waistline = rand(60, 100);
			$bloodPressureHigh = rand(100, 120);
			$bloodPressureLow = rand(80, 100);
			$totalCholesterol = rand (0, 5);
			$highDensityLipoprotein = rand(0, 2);
			$lowDensityLipoprotein = rand(0, 2);
			$triglyceride = rand(100, 220);
			$fastingBloodSugar = rand(30, 100);
			$normalBloodSugar = rand(30, 100);
			$smoking = rand(0, 1);
			$drinkingWeekFreq = rand(0, 7);
			$doseOfDrink = rand(1, 3);
			$sportsWeekFreq = rand (0, 7);
			$sportsDuration = rand(1, 3);
			$pressure = rand(1, 3);
			$date = date("Y-m-d H:i:s", strtotime("-$i days"));


			$record = array(
					"uid"=>$uid,
					"height"=>$height,
					"weight"=>$weight,
					"waistline"=>$waistline,
					"bloodPressureHigh"=>$bloodPressureHigh,
					"bloodPressureLow"=>$bloodPressureLow,
					"totalCholesterol"=>$totalCholesterol,
					"highDensityLipoprotein"=>$highDensityLipoprotein,
					"lowDensityLipoprotein"=>$lowDensityLipoprotein,
					"triglyceride"=>$triglyceride,
					"fastingBloodSugar"=>$fastingBloodSugar,
					"normalBloodSugar"=>$normalBloodSugar,
					"smoking"=>$smoking,
					"drinkingWeekFreq"=>$drinkingWeekFreq,
					"doseOfDrink"=>$doseOfDrink,
					"sportsWeekFreq"=>$sportsWeekFreq,
					"sportsDuration"=>$sportsDuration,
					"pressure"=>$pressure,
					"date"=>$date,
			);
			$this->CI->db->insert("userhealthinfo", $record);
		}
	}
}

class Admin_model extends CI_Model
{
	var $health_info_forge;
	var $user_forge;
	var $test_data_forge;

	public function __construct()
	{
		parent::__construct();
		$this->load->dbforge();
		$this->health_info_forge = new health_info_forge();
		$this->user_forge = new user_forge();
		$this->test_data_forge = new test_data_forge();
	}

	public function get_all_user_info()
	{
		$this->db->select("uid, nickname, lastloginat");
		$query = $this->db->get("userlogin");
		$result = array();
		for($i = 0; $i < $query->num_rows(); $i ++)
		{
			$row = $query->row_array($i);
			$result[] = $row;
		}

		return $result;
	}



	public function remove_user($uid)
	{
		$this->user_forge->remove_share_info($uid);
		$this->user_forge->remove_health_info($uid);
		$this->user_forge->remove_base_info($uid);
		$this->user_forge->remove_friend($uid);
		$this->user_forge->remove_health_info_push($uid);
		$this->user_forge->remove_symptom($uid);
		$this->user_forge->remove_symptom_push($uid);
		$this->user_forge->remove_login($uid);
	}

	public function remove_all_user()
	{
		$all_user_info = $this->get_all_user_info();
		while($user_info = current($all_user_info))
		{
			$uid = $user_info["uid"];

			$this->remove_user($uid);

			next($all_user_info);
		}
		
		$this->user_forge->remove_friend_request();
	}

	public function get_health_info()
	{
		$query = $this->db->get("healthfieldmeta");
		$health_fields = array();
		$health_desc = array();
		$health_field_type = array();

		for($i = 0; $i < $query->num_rows(); $i ++)
		{
			$row = $query->row_array($i);
			$field = $row["columnName"];
			$desc = $row["description"];
			$type = get_health_info_type($row["columnType"]);
			$health_fields[] = $field;
			$health_desc[] = $desc;
			$health_field_type[] = $type;
		}

		return array("field"=>$health_fields, "desc" => $health_desc, "type" => $health_field_type);
	}


	public function gen_test_data($num, $prefix)
	{
		$this->test_data_forge->do_data_generation($num, $prefix);
	}

	public function add_health_info($field, $type, $desc)
	{
		$this->health_info_forge->add_user_health_info($field, $type);
		$this->health_info_forge->add_health_field_meta($field, $type, $desc);
		$this->health_info_forge->add_user_share($field);
	}

	public function remove_health_info($field)
	{
		$this->health_info_forge->remove_health_field_meta($field);
		$this->health_info_forge->remove_user_health_info($field);
		$this->health_info_forge->remove_user_share($field);
	}
}
?>